﻿namespace UFIDA.U9.Cust.SH.SHFastShipBP
{
	using System;
	using System.Collections.Generic;
	using System.Text; 
	using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFIDA.U9.CBO.SCM.Supplier;
    using UFSoft.UBF.Business;
    using UFIDA.U9.CBO.Enums;
    using UFIDA.U9.Base;


	/// <summary>
	/// UpdateSupplierRelation partial 
	/// </summary>	
	public partial class UpdateSupplierRelation 
	{	
		internal BaseStrategy Select()
		{
			return new UpdateSupplierRelationImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class UpdateSupplierRelationImpementStrategy : BaseStrategy
	{
		public UpdateSupplierRelationImpementStrategy() { }

		public override object Do(object obj)
		{						
			UpdateSupplierRelation bpObj = (UpdateSupplierRelation)obj;
			
			//get business operation context is as follows
			//IContext context = ContextManager.Context	
			
			//auto generating code end,underside is user custom code
			//and if you Implement replace this Exception Code...
			//throw new NotImplementedException();

            if (bpObj.PaySupplierID == bpObj.AccrueSupplierID) return null;

            using (ISession session = Session.Open())
            {
                Supplier pay = Supplier.Finder.FindByID(bpObj.PaySupplierID);
                if (pay == null) throw new Exception("付款供应商查找失败！");
                Supplier accrue = Supplier.Finder.FindByID(bpObj.AccrueSupplierID);
                if (accrue == null) throw new Exception("立账供应商查找失败！");

                string opath = "";
                opath = "(Supplier={0} and RelationSupplier={1}) or (Supplier={2} and RelationSupplier={3})";
                opath = string.Format(opath, pay.ID, accrue.ID, accrue.ID,pay.ID);
                SupplierRelation relation = SupplierRelation.Finder.Find(opath, null);
                if (relation == null)
                {
                    relation = SupplierRelation.Create();
                    relation.RelationDirectionType = RelationDirectionTypeEnum.Horizontal;
                    relation.Effective.EffectiveDate = DateTime.Now.AddDays(-1);
                    relation.Effective.DisableDate = DateTime.MaxValue;
                    relation.Effective.IsEffective = true;
                    relation.RelationSupplierKey = pay.Key;
                    relation.SupplierKey = accrue.Key;
                    relation.OrgKey = Context.LoginOrg.Key;

                }

                session.Commit();
            }

            return null;
		}		
	}

	#endregion
	
	
}